<?php
/**
 * Created by PhpStorm.
 * User: clcw908
 * Date: 2017/1/12
 * Time: 15:52
 */
namespace AuctionBack\Controller;

use AuctionBack\Controller\BaseController;
use AuctionBack\Logic\RegionLogic;
use AuctionCore\Company\CompanyManage;
use AuctionBack\Logic\IdentityLogic;
use AuctionCore\Account\Back\EmployeeList;
use AuctionCore\Account\Back\Employee;
use AuctionBack\Logic\RecheckLogic;

/**
 *  身份控制器
 *
 * Class IdentityController
 * @package App\AuctionBack\Controller
 */
class IdentityController extends BaseController {

	/**
	 * IdentityController constructor.
	 */
	public function __construct()
	{
		parent::__construct();
		$bc_list = $this->get_branch_company_list();
		$this->assign('bc_list', $bc_list);
	}

	/**
	 * index
	 */
	public function index(){
		$this->display();
	}

	/**
	 * 身份列表
	 */
	public function getList(){
	/*	$page = I("get.p/d", 1);
		$size = 10;*/
		$developerAuthName = 'KaiFaRen'; //开发人
		$e = new Employee($this->user_info['username']);

		$developerResult = $e->check_auth($developerAuthName);

		$watchAuthName = 'WeiHuRen'; //维护人
		$watchResult = $e->check_auth($watchAuthName);
		$auth = [
			'developer' => $developerResult, //开发人
			'watcher' => $watchResult, //维护人
		];

		$params = [];
		$result = IdentityLogic::getList();
		$this->assign('auth',$auth);
		$this->assign('list', $result['list']);
		$this->assign("params", $params);
		$this->display('Identity/identity_list');
	}

	/**
	 * 身份详情
	 */
	public function detail(){
		$identityId = I("get.id/d");
		$branchId = I("get.branch_id",'');
		$identityList = IdentityLogic::getList();
		$name = '';
		foreach ($identityList['list'] as $key => $value){
			if($identityId == $value['id']){
				$name = $value['value'];
				break;
			}
		}
		if(empty($name)){
			$this->error('参数错误!');
		}

		$emp_list = new EmployeeList();
		$where = "";
		if(!empty($branchId)){
			$branchId = $branchId;
		}else{
			$branchId = $this->user_info['branch_id'];
		}
		if ($branchId > 0) {
			$where .= " and branch_id = $branchId";
		}
		$emp_list->condition = ' where status in (0,1) ' . $where;
		$list = $emp_list->getAll();
		$empArr = [];
		foreach($list as $key => $value){
			$empArr[] = $value['emp_id'];
		}

		$identityEmpList = [];
		if(!empty($empArr)){
			$identityEmpList = IdentityLogic::getEmployeeByIdentityId($identityId,$empArr);
		}

		foreach ($identityEmpList as $key=>&$value){
			$emp_id = $value['emp_id'];
			foreach ($list as $k => $v){
				$emp_new_id = $v['emp_id'];
				if($emp_id == $emp_new_id){
					$value['real_name'] = $v['real_name'];
					$value['username'] = $v['username'];
					$value['mobile'] = $v['mobile'];
					break;
				}
			}
		}



		$this->assign('identity_emp_List',$identityEmpList);
		$this->assign('identity_id',$identityId);
		$this->assign('branch_id',$branchId);
		$this->assign('name',$name);
		$this->display('Identity/identity_info');
	}

	public function searchEmp(){
		$search_key = trim(I('post.search_key',''));
		$identityId = I('post.identity_id');
		$branchId = I("post.branch_id",'');

		$identityEmpList = IdentityLogic::getEmployeeByIdentityId($identityId,[]);

		$emp_list = new EmployeeList();
		$where = "";
		if ($search_key) {
			$where .= " and (real_name='{$search_key}' or mobile='{$search_key}' or username='{$search_key}') ";
		}

		if(!empty($branchId)){
			$branchId = $branchId;
		}else{
			$branchId = $this->user_info['branch_id'];
		}
		if ($branchId > 0) {
			$where .= " and branch_id = $branchId";
		}
		$emp_list->condition = ' where status in (0,1) ' . $where;
		$list = $emp_list->getAll();

		foreach ($list as $key => &$value){
			$empId = $value['emp_id'];
			foreach ($identityEmpList as $k => $v){
				$newEmpId = $v['emp_id'];
				if($empId == $newEmpId){
					$value['is_add'] = true; //表示已经添加过了
					break;
				}
			}
		}
		$this->helpJsonResult(200,'搜索成功!',$list);
	}


	/**
	 * 添加身份
	 */
	public function add(){
		$title = I('post.title'); //身份名称
		$remark = I('post.remark'); //描述
		$data =[
			'title' => $title,
			'remark'=> $remark,
		];

		$result = IdentityLogic::add($data);
		if(!$result){
			$this->helpJsonResult('422','添加身份信息失败!');
		}else{
			$this->helpJsonResult('200','添加成功!');
		}
	}

	/**
	 * 编辑身份
	 */
	public function edite(){
		$id = I('post.id');
		$title = I('post.title'); //身份名称
		$remark = I('post.remark'); //描述
		$data =[
			'title' => $title,
			'remark'=> $remark,
		];

		$identityObj = new IdentityLogic($id);
		$result = $identityObj->edite($data);
		if(!$result){
			$this->helpJsonResult('422','编辑身份信息失败!');
		}else{
			$this->helpJsonResult('200','编辑成功!');
		}
	}


	/**
	 * 添加员工身份关系
	 */
	public function addEmpIdentityRelation(){
		$identityId = I('post.identity_id');
		$delArr = I('post.del_arr');
		$addArr = I('post.add_arr');

		M()->startTrans();
		foreach ($delArr as $key => $value){
			$data = [
				'emp_id' => $value,
				'identity_id' => $identityId,
			];
			$result = IdentityLogic::delEmpIdentityRelation($data);
			if(!$result){
				M()->rollback();
				$this->helpJsonResult(422,'删除员工身份关系失败!');
			}
		}

		$lastAddArr = [];
		foreach ($addArr as $key => $value){
			$lastAddArr[] = [
				'emp_id' => $value,
				'identity_id'=>$identityId,
			];
		}

		if(!empty($lastAddArr)){
			$result = IdentityLogic::addEmpIdentityRelation($lastAddArr);
			if($result){
				M()->commit();
				$this->helpJsonResult(200,'操作成功!');
			}else{
				M()->rollback();
				$this->helpJsonResult(422,'添加员工身份关系失败!');
			}
		}else{
			M()->commit();
			$this->helpJsonResult(200,'操作成功!');
		}

	}


	/**
	 * 删除 员工 身份关系
	 */
	public function delEmpIdentityRelation(){
		$empId = I('post.emp_id');
		$identityId = I('post.identity_id');
		$data = [
			'emp_id' => $empId,
			'identity_id' => $identityId,
		];
		$result = IdentityLogic::delEmpIdentityRelation($data);
		if($result){
			$this->helpJsonResult(200,'操作成功!');
		}else{
			$this->helpJsonResult(422,'删除员工身份关系失败!');
		}
	}

	/**
	 * 根据身份编号，得到员工信息
	 */
	public function getEmpByIdentityId(){
		$identityId = I('post.identity_id');
		$page = I("get.p/d", 1);
		$size = 10;
		//$parameter
		$parameters = [];

		$parameters['identity_id'] = $identityId;

		IdentityLogic::getEmployeeByIdentityId($parameters);
	}


	/**
	 *  通过员工编号，得到所属片区数量
	 */
	public function getRegionByEmpId(){
		$emp_id = I('post.emp_id');
		$count = RegionLogic::getRegionCount($emp_id);//维护人所属片区数量
		if($count >0){
			$this->helpJsonResult(422,'请先删除此维护人与片区的关联!');
		}else{
			$this->helpJsonResult(200,'操作成功');
		}
	}


}